package com.glory.brepda.activity.mfg;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.graphics.PointF;
import android.view.MotionEvent;
import android.view.ScaleGestureDetector;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.glory.brepda.R;
import com.glory.brepda.base.BaseActivity;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_SharePreferencesUtils;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.IOException;

import androidx.core.content.ContextCompat;
import butterknife.BindView;

public class ImageUtilsActivity extends BaseActivity implements View.OnTouchListener {

    @BindView(R.id.image)
    ImageView image;
    @BindView(R.id.llMain)
    LinearLayout llMain;
    // 縮放控制
    private Matrix matrix = new Matrix();
    private Matrix savedMatrix = new Matrix();

    // 不同状态的表示：
    private static final int NONE = 0;
    private static final int DRAG = 1;
    private static final int ZOOM = 2;
    private int mode = NONE;

    // 定义第一个按下的点，两只接触点的重点，以及出事的两指按下的距离：
    private PointF startPoint = new PointF();
    private PointF midPoint = new PointF();
    private float oriDis = 1f;
    String FILE_STREAM = "";

    private static long lastClickTime; //点击时间 hwb001

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_imageutils;
    }

    @Override
    protected void initView() {
        setMainLayout(llMain);

        FILE_STREAM = FreeApi_SharePreferencesUtils.getSharePre("DG_CPC_FILE", "FILE_STREAM","" , com.glory.brepda.base.MyApp.app);
        try {
            showImage(FILE_STREAM,image);
        } catch (IOException e) {
            e.printStackTrace();
        }
//        image.txt.setImageDrawable(ContextCompat.getDrawable(getApplicationContext(),R.drawable.a1));
        image.setOnTouchListener(this);
    }

    @Override
    protected void initData() {

    }

    // 计算两个触摸点之间的距离
    private float distance(MotionEvent event) {
        float x = event.getX(0) - event.getX(1);
        float y = event.getY(0) - event.getY(1);
        return Float.valueOf(String.valueOf(Math.sqrt(x * x + y * y))) ;
    }

    // 计算两个触摸点的中点
    private PointF middle(MotionEvent event) {
        float x = event.getX(0) + event.getX(1);
        float y = event.getY(0) + event.getY(1);
        return new PointF(x / 2, y / 2);
    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {

        ImageView view = (ImageView) v;
        switch (event.getAction() & MotionEvent.ACTION_MASK) {
            //双击
            case MotionEvent.ACTION_UP:
                //点击整个页面都会让内容框获得焦点，且弹出软键盘
                if(isFastDoubleThreeClick()) {
                    finish();
                }else{
                    mode = NONE;
                }
                break;
            // 单指
            case MotionEvent.ACTION_DOWN:
                matrix.set(view.getImageMatrix());
                savedMatrix.set(matrix);
                startPoint.set(event.getX(), event.getY());
                mode = DRAG;
                break;
            // 双指
            case MotionEvent.ACTION_POINTER_DOWN:
                oriDis = distance(event);
                if (oriDis > 10f) {
                    savedMatrix.set(matrix);
                    midPoint = middle(event);
                    mode = ZOOM;
                }
                break;
            // 手指放开
            // case MotionEvent.ACTION_UP:
            case MotionEvent.ACTION_POINTER_UP:
                mode = NONE;
                break;
            // 单指滑动事件
            case MotionEvent.ACTION_MOVE:
                if (mode == DRAG) {
                    // 是一个手指拖动
                    matrix.set(savedMatrix);
                    matrix.postTranslate(event.getX() - startPoint.x, event.getY() - startPoint.y);
                } else if (mode == ZOOM) {
                    // 两个手指滑动
                    float newDist = distance(event);
                    if (newDist > 10f) {
                        matrix.set(savedMatrix);
                        float scale = newDist / oriDis;
                        matrix.postScale(scale, scale, midPoint.x, midPoint.y);
                    }
                }
                break;
        }
        // 设置ImageView的Matrix
        view.setImageMatrix(matrix);
        return true;
    }



    /**
     * 防止过快点击(200毫秒)
     * @return
     */
    private static boolean isFastDoubleThreeClick() {
        long time = System.currentTimeMillis();
        long timeD = time - lastClickTime;
        if ( 0 < timeD && timeD < 200) {    //这样所有按钮在200毫秒内不能同时起效。
            return true;
        }
        lastClickTime = time;
        return false;
    }

    public static void showImage(String uri, ImageView iv) throws IOException {
        FileInputStream fs = new FileInputStream(uri);
        BufferedInputStream bs = new BufferedInputStream(fs);
        Bitmap btp = BitmapFactory.decodeStream(bs);
        iv.setImageBitmap(btp);
        bs.close();
        fs.close();
        btp = null;
    }


}
